'''
copied from http://arainyday.se/projects/python/HexagonExample/
The GUI. Extended the code to fit special properties of Pentalath.
'''
 
import os, pygame,math
from pygame.locals import *
from game.datastructure.PlayerColor import PlayerColor
from game.GameServer import GameServer
from game.player.human_player.HumanPlayer import HumanPlayer
import threading
from game.player.random_player.RandomPlayer import RandomPlayer
from game.player.alpha_beta_player.AlphaBetaPlayer import AlphaBetaPlayer
from game import GameLogic
import time
# This is the rectangular size of the hexagon tiles.
TILE_WIDTH =  38
TILE_HEIGHT = 41

# This is the distance in height between two rows.
ROW_HEIGHT = 31

# This value will be applied to all odd rows x value.
ODD_ROW_X_MOD = 19

# This is the size of the square grid that will help us convert pixel locations to hexagon map locations.
GRID_WIDTH = 38
GRID_HEIGHT = 31

# This is the modification tables for the square grid.

a1=(-1,-1)
b1=(0,0)
c1=(0,-1)

gridEvenRows = [
[a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,b1,b1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1],
[a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,b1,b1,b1,b1,b1,b1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1],
[a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1],
[a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1],
[a1,a1,a1,a1,a1,a1,a1,a1,a1,a1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1],
[a1,a1,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c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2],
[a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2],
[a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2],
[a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2],
[a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,a2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2]
]

CALC_TIME = 20

class PentalathGUI:

    def pixelToHexMap(self,x,y):
        """
        Converts a pixel location to a location on the hexagon map.
        """        

        # Get the square location in our help grid.
        gridX = x/GRID_WIDTH
        gridY = y/GRID_HEIGHT
            
        # Calculate the pixel location within that square
        gridPixelX = x%GRID_WIDTH
        gridPixelY = y%GRID_HEIGHT

        # Update the gridRect to show the correct location in the grid
        self.gridRect.topleft = (gridX*GRID_WIDTH,gridY*GRID_HEIGHT)
       
        # Apply the modifiers to get the correct hexagon map location.
        if gridY&1:
            # Odd rows
            hexMapX=gridX+gridOddRows[gridPixelY][gridPixelX][0]
            hexMapY=gridY+gridOddRows[gridPixelY][gridPixelX][1]
        else:
            # Even rows
            hexMapX=gridX+gridEvenRows[gridPixelY][gridPixelX][0]
            hexMapY=gridY+gridEvenRows[gridPixelY][gridPixelX][1]
        if hexMapY < self.gameserver.field.height/2 - 1:
            hexMapX = hexMapX - 1 - (hexMapY == 0)
        elif hexMapY > self.gameserver.field.height/2:
            hexMapX = hexMapX + 1 + (hexMapY > self.gameserver.field.height/2+2)
        return (hexMapX,hexMapY)

    def hexMapToPixel(self,mapX,mapY):
        """
        Returns the top left pixel location of a hexagon map location.
        """
        # change pixel because of the special form of the pentalath field
#        if mapY < self.field.height/2:
#            mapX = mapX + self.field.topwidth - mapY - (self.field.height % 2 == 1)
#        elif mapY > self.field.height/2:
#            mapX = mapX - 1
        if mapY < self.gameserver.field.height/2 - 1:
            mapX = mapX + 1 + (mapY == 0)
        elif mapY > self.gameserver.field.height/2:
            mapX = mapX - 1 - (mapY > self.gameserver.field.height/2+2)
        if mapY & 1:
            # Odd rows will be moved to the right.
            return (mapX*TILE_WIDTH+ODD_ROW_X_MOD,mapY*ROW_HEIGHT)
        else:
            return (mapX*TILE_WIDTH,mapY*ROW_HEIGHT)
            

    def drawMap(self):       
        """
        Draw the tiles. This method has been extended to fit the special properties
        of a Pentalath field.
        """

        self.mapimg = pygame.Surface((640,480),1)
        self.mapimg= self.mapimg.convert()
        self.mapimg.fill((104,104,104))
        for y in range(self.gameserver.field.height):
            for x in range(len(self.gameserver.field.field[y])):
                if self.gameserver.field.field[y][x] is None:
                    continue
                #if not ((y < height/2 and x < topwidth + y) or (y >= height/2 and x >= y - topwidth + ifHalfWidthIsOdd)):
                #    continue
                # Get the top left location of the tile.
                pixelX,pixelY = self.hexMapToPixel(x,y)
                

                # Blit the tile to the map image.
                self.mapimg.blit(self.tile,(pixelX,pixelY))

                # Show the hexagon map location in the center of the tile.
                
                #lrect=location.get_rect()
                #lrect.center = (pixelX+(TILE_WIDTH/2),pixelY+(TILE_HEIGHT/2))             
                #self.mapimg.blit(location,lrect.topleft)
                
    def setValue(self):
        self.drawMap()
        for y in range(self.gameserver.field.height):
            for x in range(self.gameserver.field.width):
                pcolor = self.gameserver.field.get(x,y)
                if pcolor is not None and pcolor != PlayerColor.NONE:
                    color = (255, 255, 255)
                    if pcolor == PlayerColor.BLACK:
                        color = (0,0,0) 
                    pixelX,pixelY = self.hexMapToPixel(x,y)
                    pygame.draw.circle(self.mapimg, color, (pixelX+TILE_WIDTH/2, pixelY+TILE_HEIGHT/2), 10, 0)
    
    def loadTiles(self):
        """
        Load the tile and the cursor.
        """
        self.tile = pygame.image.load("./data/hextile.png").convert()
        self.tile.set_colorkey((0x80, 0x00, 0x80), RLEACCEL)

        self.cursor = pygame.image.load("./data/hexcursor.png").convert()
        self.cursor.set_colorkey((0x80, 0x00, 0x80), RLEACCEL)                
        self.cursorPos = self.cursor.get_rect()
        
    def __init__(self, gameserver):
        self.gameserver = gameserver
    
    def init(self):
        """
        Setup the screen etc.
        """
        self.screen = pygame.display.set_mode((640, 480),1)
        pygame.display.set_caption('Press SPACE to toggle the gridRect display')
    
        self.loadTiles()
        self.drawMap()        

        self.gridRect = pygame.Rect(0,0,GRID_WIDTH,GRID_HEIGHT)

    def setCursor(self,x,y):
        """
        Set the hexagon map cursor.
        """
        mapX,mapY = self.pixelToHexMap(x,y)
        pixelX,pixelY = self.hexMapToPixel(mapX,mapY)        
        self.cursorPos.topleft = (pixelX,pixelY)

    def doit(self, stopEvent):
        self.humanPlayer = self.gameserver.currentPlayer().isHumanPlayer()
        if self.gameserver.currentPlayer().stopable():
            self.gameserver.currentPlayer().setStopEvent(stopEvent)
        self.won,action,color = self.gameserver.nextAction()
        self.setValue()
        self.humanPlayer = False
        self.doAction = True
        print self.doAction
        if self.won:
            print color, " has won!"
        print "Action done"
    
    def stopit(self, stopEvent):
        time.sleep(CALC_TIME)
        stopEvent.set()

    def mainLoop(self):
        pygame.init()
        self.init()
        self.doAction = True
        self.humanPlayer = False
        
        self.clock = pygame.time.Clock()
                        
        self.showGridRect = False
        self.won = None
        self.finished = False
        while True:
            if self.won != None:
                self.finished = True
                self.doAction = False
            if self.doAction:
                print "DOING"
                self.doAction = False
                self.stopEvent = threading.Event()
                # Work thread
                workthread = threading.Thread(target=self.doit, args=[self.stopEvent])
                # Stop thread
                stopthread = threading.Thread(target=self.stopit, args=[self.stopEvent])
                workthread.start()
                stopthread.start()
            for event in pygame.event.get():
                if event.type == QUIT:
                    return
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        return               
                    elif event.key == K_n:
                        self.gameserver.newGame()
                        self.drawMap()
                        self.finished = False
                        self.won = None
                        self.doAction = True
#                    elif event.key == K_SPACE and not self.finished and self.humanPlayer:
#                        self.gameserver.humanPlayerEvent(GameLogic.SWAP_ACTION)
                    elif event.key == K_l:
                        self.gameserver.load()
                        self.setValue()
                
                elif event.type == MOUSEMOTION:
                    self.setCursor(event.pos[0],event.pos[1])
                    
                elif (not self.finished) and self.humanPlayer and event.type == pygame.locals.MOUSEBUTTONUP:
                    self.gameserver.humanPlayerEvent(self.pixelToHexMap(event.pos[0], event.pos[1]))
        
        
            # DRAWING             
            self.screen.blit(self.mapimg, (0, 0))
            self.screen.blit(self.cursor,self.cursorPos)                  
            if self.showGridRect:
                pygame.draw.rect(self.screen, (0xff,0xff,0xff), self.gridRect, 1)
            pygame.display.flip()
            
def main():
    player1 = HumanPlayer()
    player2 = AlphaBetaPlayer()
    gameserver = GameServer(player1, player2)
    g = PentalathGUI(gameserver)
    if player1.isHumanPlayer():
        player1.setGUI(g)
    if player2.isHumanPlayer():
        player2.setGUI(g)
    g.mainLoop()

 
#this calls the 'main' function when this script is executed
if __name__ == '__main__': main()
